$(document).ready(function () {
    initClazz();
    $("#register").click(function () {
        var name = $("#username").val();
        var password = $("#password").val();
        var confirmpassword = $("#confirmpassword").val();
        var clazzId=$("#selectclazz").val();
        var email = $("#email").val();
        var myReg = /^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;
        if (name == null || name == "" || password == null || password == "" || confirmpassword == null || confirmpassword == "" ||
            email == null || email == "") {
            alert("你还有东西没有填写完毕！");
        } else {
            if (!myReg.test(email)) {
                alert(email)
                alert("邮箱格式不对!");
            } else {
                if (password != confirmpassword) {
                    alert("密码不一致！");
                } else {
                    if(clazzId==0) {
                        alert("未选择班级");
                    }else {
                        $.ajax({
                            type: "POST",
                            url: "/register/getUser",
                            dataType: "json",
                            async: false,
                            data: {
                                name: name,
                                email: email,
                            },
                            success: function (data) {
                                if (name == data.name) {
                                    alert("用户名已存在，请重新输入！")
                                }else {
                                    if (email == data.email) {
                                        alert("邮箱被占用，请重新输入！")
                                    }
                                }

                            },
                            error: function () {
                                $.ajax({
                                    type: "POST",
                                    url: "/register/addUser",
                                    dataType: "json",
                                    data: {
                                        name: name,
                                        password: password,
                                        email: email,
                                        clazzid:clazzId,
                                    },
                                    success: function () {
                                        alert("注册成功！")
                                        window.location.href="login";
                                    },
                                    error: function () {
                                        alert("注册失败！")
                                    },
                                })
                            }
                        })
                    }

                }
            }
        }
    })

    function initClazz(){
        $.ajax({
            type:"get",
            url:"/register/getAllClazz",
            dataType: "json",
            success:function (data) {
                $("#selectclazz").prepend("<option value='0'>请选择班级</option>");
                for(var i in data){
                    $("#selectclazz").append("<option value="+data[i].id+">"+data[i].name+"</option>");
                }
            }
        })

    }
})